package com.ambitor.ipro.util;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;

public class LocalConnectionUtils {

	private static boolean isInited = false;

	private static BasicDataSource ds = new BasicDataSource();

	public static Connection getConnection() throws SQLException {
		if (isInited == false)
			setupDataSource();
		return ds.getConnection();
	}

	private static void setupDataSource() {
		ds.setDriverClassName(PropertiesReader.getString("DRIVER_NAME"));
		ds.setUsername(PropertiesReader.getString("USERNAME"));
		ds.setPassword(PropertiesReader.getString("PASSWORD"));
		ds.setUrl(PropertiesReader.getString("CONNECT_URI"));
		ds.setTestOnBorrow(true);
		ds.setValidationQuery("SELECT 1 FROM DUAL");
		isInited = true;
	}

	public static void main(String[] args) throws Exception {
		Connection conn = LocalConnectionUtils.getConnection();
		SuperDao dao = new SuperDaoImpl(conn);
		System.out.println(dao.queryList("sql_sql").toString());
	}
}
